package com.jservice.web.callBack;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Clear;
import com.jservice.framework.annotation.RouteBind;
import com.jservice.framework.authc.AuthcInterceptor;
import com.jservice.web.base.JBaseController;
import com.jservice.web.smkRecharge.SmkReChargeService;
import com.jservice.web.smkRefund.SmkReFundService;
import com.jservice.web.smkSms.SmsService;

/**
 * CallBackController
 *
 * @Author cwledit
 * @DATE 2017/6/21.
 */


@RouteBind(path = "/api/callback")
public class CallBackController extends JBaseController{
    /**
     * 充值回调接口
     */
    @Clear(AuthcInterceptor.class)
    public void recharge(){
        String biz_content = getPara("biz_content");
        JSONObject jsonObject = JSON.parseObject(biz_content);
        String out_trade_no = jsonObject.getString("out_trade_no");
        String trade_status = jsonObject.getString("trade_status");
        String trade_no = jsonObject.getString("trade_no");
        String total_amount = jsonObject.getString("total_amount");
        if (SmkReChargeService.me().updateStatus(out_trade_no,trade_status,trade_no,total_amount)){
            renderText("success");
        }else{
            renderText("failure");
        }
    }

    /**
     * 充值退款结果
     */
    @Clear(AuthcInterceptor.class)
    public void refund(){
        String biz_content = getPara("biz_content");
        JSONObject jsonObject = JSON.parseObject(biz_content);
        String out_refund_no = jsonObject.getString("out_refund_no");
        String trade_status = jsonObject.getString("trade_status");
        String refund_trade_no = jsonObject.getString("refund_trade_no");
//        String trade_no = jsonObject.getString("trade_no");
        String refund_reason = jsonObject.getString("refund_reason");
        if (SmkReFundService.me().updateStatus(out_refund_no,trade_status,refund_trade_no, refund_reason)){
            renderText("success");
        }else {
            renderText("failure");
        }

    }

    /**
     * 推送消息通知
     */
    @Clear(AuthcInterceptor.class)
    public void smspush(){
        String biz_content = getPara("biz_content");
        JSONObject jsonObject = JSON.parseObject(biz_content);
        String account = jsonObject.getString("account");
        String message = jsonObject.getString("message");
        if (SmsService.me().save(account,message)){
            renderText("success");
        }else {
            renderText("failure");
        }
    }

}
